﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WastedMechanics
{
    public class GuiList : GuiControl
    {                        
        public enum ListOrientation
        {
            Vertical,
            Horizontal
        }

        public List<GuiControl> Items;

        public int ScrollPosition;
        public ListOrientation Orientation;

        public event Predicate<GuiControl> OnItemClick;

        public GuiList(string name)
            : base(name)
        {
            Items = new List<GuiControl>();
        }

        public void PlugItem(GuiControl item)
        {
            item.AttachTo(this);
            item.OnClick += OnItemClick;
            item.OnDragBegin += OnItemDragBegin;
            Items.Add(item);
        }

        public void UnplugItem(GuiControl item)
        {            
            item.Detach();
            item.OnClick -= OnItemClick;
            item.OnDragBegin -= OnItemDragBegin;
            Items.Remove(item);
            UpdateInternals();
        }

        public void ClearItems()
        {
            Items.ForEach(item => item.Destroy());            
            Items.Clear();
        }

        private static void OnItemDragBegin(GuiControl item)
        {
            GuiList list = item.Parent as GuiList;
            list.UnplugItem(item);
        }

        protected override bool ProcessInput()
        {
            if (base.ProcessInput())
            {
                if (IsMouseIn)               
                    ScrollPosition -= System.Math.Sign(Engine.MouseInput.ScrollAmount);  

                return true;
            }
            else
                return false;
        }

        protected override void UpdateInternals()
        {
            base.UpdateInternals();

            if (Items.Count == 0)
                return;

            ScrollPosition = Helper.Clamp(0, Items.Count-1, ScrollPosition);            

            // hiding items before ScrollPosition
            for (int i = 0; i < ScrollPosition; i++)
                Items[i].IsVisible = false;
            
            float itemOffset = 0.0f;            
            for (int i = ScrollPosition; i < Items.Count; i++)
            {                
                GuiControl item = Items[i];                
                                
                if (Orientation == ListOrientation.Horizontal)
                {
                    if (itemOffset + item.Width < Width && item.Height < Height)
                    {
                        item.IsVisible = true;
                        item.Left = itemOffset;
                        item.Top = 0;
                    }
                    else
                        item.IsVisible = false;
                    itemOffset += item.Width;
                }
                else
                {
                    if (itemOffset + item.Height < Height && item.Width < Width)
                    {
                        item.IsVisible = true;
                        item.Top = itemOffset;
                        item.Left = 0;
                    }
                    else
                        item.IsVisible = false;
                    itemOffset += item.Height;
                }                
            }            
        }
    }
}
